<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\File;

use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;

/**
 * Description of Upload
 *
 * @author Allen <allen@lg4.cn>
 */
class UploadedFile extends SymfonyUploadedFile {

    public $storeFileName;

    public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false) {
        parent::__construct($path, $originalName, $mimeType, $size, $error, $test);
        $this->storeFileName = $this->getClientOriginalName();
    }

    public function store($path, $disk = null) {
        $this->move($path, $this->getStoreFileName());
        return $this;
    }

    public function randomFileName() {
        $this->storeFileName = md5(str_random() . date('Y-m-d H:i:s u')) . '.' . $this->getClientOriginalExtension();
        return $this;
    }

    public function getStoreFileName() {
        $this->storeFileName;
    }

    public static function createFromBase(SymfonyUploadedFile $file, $test = false) {
        return $file instanceof static ? $file : new static(
                $file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $file->getError(), $test
        );
    }

}
